package com.aperos.webservice;

import java.io.IOException;

import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class EditBarServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(EditBarServlet.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	try {
	    	boolean flag = true;
	    	resp.setContentType("text/plain");
	    	
	    	String id = req.getParameter("id");
	    	if (id == null || "".equals(id)) {
	    		resp.getWriter().println("failed|Id is required");
	    		flag = false;
	    	}
	
	    	String name = req.getParameter("name");
	    	if (name == null || "".equals(name)) {
	    		resp.getWriter().println("failed|Name is required");
	    		flag = false;
	    	} else {
	    		name = req.getParameter("name").replace("\n", ", ");
		    	name = name.replace("|", " ");
	    	}
	    	
	    	String address = req.getParameter("address");
	    	if (address == null || "".equals(address)) {
	    		address = null;
	    	} else {
	    		address = req.getParameter("address").replace("\n", ", ");
		    	address = address.replace("|", " ");
	    	}
	    	
	    	String priceBeer = req.getParameter("priceBeer");
	    	Double barPriceBeer = null;
	    	if (priceBeer == null || "".equals(priceBeer)) {
	    		resp.getWriter().println("failed|Beer price is required");
	    		flag = false;
	    	} else {
	    		barPriceBeer = Double.valueOf(priceBeer).doubleValue();
	    	}
	    	
	    	if (flag) {
		    	String priceCocktail = req.getParameter("priceCocktail");
		    	Double barPriceCocktail = null;
		    	if (priceCocktail != null && !"".equals(priceCocktail)) {
		    		barPriceCocktail = Double.valueOf(priceCocktail).doubleValue();
		    	} else {
		    		barPriceCocktail = null;
		    	}
		    	
		    	String happyHourBeginHour = req.getParameter("happyHourBeginHour");
		    	Integer barHappyHourBeginHour = null;
		    	if (happyHourBeginHour != null && !"".equals(happyHourBeginHour)) {
		    		barHappyHourBeginHour = Integer.valueOf(happyHourBeginHour).intValue();
		    	} else {
		    		barHappyHourBeginHour = null;
		    	}
		    	
		    	String happyHourBeginMinute = req.getParameter("happyHourBeginMinute");
		    	Integer barHappyHourBeginMinute = null;
		    	if (happyHourBeginMinute != null && !"".equals(happyHourBeginMinute)) {
		    		barHappyHourBeginMinute = Integer.valueOf(happyHourBeginMinute).intValue();
		    	} else {
		    		barHappyHourBeginMinute = null;
		    	}
		    	
		    	String happyHourEndHour = req.getParameter("happyHourEndHour");
		    	Integer barHappyHourEndHour = null;
		    	if (happyHourEndHour != null && !"".equals(happyHourEndHour)) {
		    		barHappyHourEndHour = Integer.valueOf(happyHourEndHour).intValue();
		    	} else {
		    		barHappyHourEndHour = null;
		    	}
		    	
		    	String happyHourEndMinute = req.getParameter("happyHourEndMinute");
		    	Integer barHappyHourEndMinute = null;
		    	if (happyHourEndMinute != null && !"".equals(happyHourEndMinute)) {
		    		barHappyHourEndMinute = Integer.valueOf(happyHourEndMinute).intValue();
		    	} else {
		    		barHappyHourEndMinute = null;
		    	}
		    	
		    	String priceBeerHappyHour = req.getParameter("priceBeerHappyHour");
		    	Double barPriceBeerHappyHour = null;
		    	if (priceBeerHappyHour != null && !"".equals(priceBeerHappyHour)) {
		    		barPriceBeerHappyHour = Double.valueOf(priceBeerHappyHour).doubleValue();
		    	} else {
		    		barPriceBeerHappyHour = null;
		    	}
		    	
		    	
		    	String priceCocktailHappyHour = req.getParameter("priceCocktailHappyHour");
		    	Double barPriceCocktailHappyHour = null;
		    	if (priceCocktailHappyHour != null && !"".equals(priceCocktailHappyHour)) {
		    		barPriceCocktailHappyHour = Double.valueOf(priceCocktailHappyHour).doubleValue();
		    	} else {
		    		barPriceCocktailHappyHour = null;
		    	}
		    	
		    	String money = req.getParameter("money");
		    	if (money == null || "".equals(money)) {
		    		money = "€";
		    	}
		    	
		    	PersistenceManager pm = PMF.get().getPersistenceManager();
		        try {
		        	Key key = KeyFactory.createKey(Bar.class.getSimpleName(), Integer.valueOf(id).intValue());
		            Bar bar = pm.getObjectById(Bar.class, key);
		            if (bar != null) {
			            bar.setName(name);
			            bar.setAddress(address);
			            bar.setPriceBeer(barPriceBeer);
			            bar.setPriceCocktail(barPriceCocktail);
			            bar.setHappyHourBeginHour(barHappyHourBeginHour);
			            bar.setHappyHourBeginMinute(barHappyHourBeginMinute);
			            bar.setHappyHourEndHour(barHappyHourEndHour);
			            bar.setHappyHourEndMinute(barHappyHourEndMinute);
			            bar.setPriceBeerHappyHour(barPriceBeerHappyHour);
			            bar.setPriceCocktailHappyHour(barPriceCocktailHappyHour);
			            bar.setMoney(money);
			            
			            pm.makePersistent(bar);	
			            
			            resp.getWriter().println("OK");
		            }
		        } finally {
		            pm.close();
		        }
	    	} else {
	    		resp.getWriter().println("failed");
	    	}
    	} catch (Exception e) {
			e.printStackTrace();
			resp.getWriter().println("failed|Exception");
		}

    }
}